package com.example.web.config;


import com.example.common.constant.RedisConstant;
import com.example.common.redis.RedisUtils;
import com.example.system.domain.Dict;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig implements ApplicationListener<WebServerInitializedEvent> {
    private final Logger logger = LoggerFactory.getLogger(SwaggerConfig.class);
    @Value("${srppms.name}")
    public String name;
    @Value("${srppms.version}")
    public String version;
    @Value("${srppms.author}")
    public String author;
    @Autowired
    private RedisUtils redisUtils;

    @Bean()
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("srppms")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.web.controller"))
                .build();
    }

    @Bean()
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("srppms-monitor")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.monitor.controller"))
                .build();
    }

    @Bean()
    public Docket docket3() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("srppms-common")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.common.controller"))
                .build();
    }

    @Bean()
    public Docket docket4() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("srppms-system")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.system.controller"))
                .build();
    }

    // 构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(name) //标题
                .version(version) //版本
                .description("系统API描述") //简介
                .contact(new Contact(author, "", ""))
                .build();
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        try {
            //获取IP
            String hostAddress = Inet4Address.getLocalHost().getHostAddress();
            //获取端口号
            int port = event.getWebServer().getPort();
            //获取应用名
            String applicationName = event.getApplicationContext().getApplicationName();
//            http://localhost:8888/doc.html
            String url = "http://" + hostAddress + ":" + event.getWebServer().getPort() + applicationName + "/doc.html";
            logger.info("Swagger启动成功！接口文档地址: " + url);
            List<Dict> list = new ArrayList<>();
            Dict dict = new Dict();
            dict.setDict(url);
            list.add(dict);
            String key = RedisConstant.dict + "swagger";
            redisUtils.set(key, list, -1);
        } catch (UnknownHostException e) {
//            e.printStackTrace();
            System.out.println("Exception:" + e);
        }
    }
}